package com.sugon.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.sugon.dao.ModuleDataDao;
import com.sugon.entity.*;
import com.sugon.service.*;
import com.sugon.utils.ProcessInfoEnum;
import com.sugon.utils.ShiroUtils;
import com.sugon.utils.SysMsgType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.sugon.utils.ShiroUtils.getUserId;

/**
 * 模具资料信息Service实现类
 *
 * @author sugon
 * @date 2019-07-31 10:20:50
 */
@Service("moduleDataService")
public class ModuleDataServiceImpl implements ModuleDataService {
    @Autowired
    private ModuleDataDao moduleDataDao;
    @Autowired
    private SysProcessInfoService sysProcessInfoService;
    @Autowired
    private SysMsgService sysMsgService;
    @Autowired
    private KnowBaseService knowBaseService;
    @Autowired
    private ModuleDataFileService moduleDataFileService;
    @Autowired
    private KnowFileService knowFileService;

    /**
     * queryObject
     *
     * @param id 主键
     * @return
     */
    @Override
    public ModuleDataEntity queryObject(String id) {
        return moduleDataDao.queryObject(id);
    }

    @Override
    /*    @DataFilter(userAlias = "CREATE_USER", deptAlias = "DEPT_CODE")*/
    public List<ModuleDataEntity> queryList(Map<String, Object> map) {
        return moduleDataDao.queryList(map);
    }

    @Override
    public List<ModuleDataEntity> queryListApi(Map<String, Object> map) {
        return moduleDataDao.queryList(map);
    }

    @Override
    /*    @DataFilter(userAlias = "CREATE_USER", deptAlias = "DEPT_CODE")*/
    public int queryTotal(Map<String, Object> map) {
        return moduleDataDao.queryTotal(map);
    }

    @Override
    public int save(ModuleDataEntity moduleData) {
        moduleData.setId(RandomUtil.randomString(32));
        moduleData.setCreateUser(getUserId());
        moduleData.setCreateTime(new Date());
        moduleData.setDeptCode(ShiroUtils.getDeptId());
        //添加审核 消息提醒
        sysProcessInfoService.saveProcessInfo(moduleData.getId(), ProcessInfoEnum.MDE_MJZL, 0, null);
        int rows = moduleDataDao.save(moduleData);
        if (rows > 0) {
            SysMsgEntity msgEntity = new SysMsgEntity();
            String Id = moduleData.getId();
            msgEntity.setDataId(Id);
            msgEntity.setDataUrl("../module/moduledata.html?Id=" + Id);
            sysMsgService.addApproveItem(ProcessInfoEnum.MDE_MJZL, msgEntity, 0);
        }
        return rows;
    }

    @Override
    public int update(ModuleDataEntity moduleData) {
        moduleData.setUpdateUser(getUserId());
        moduleData.setUpdateTime(new Date());
        int rows = moduleDataDao.update(moduleData);
        if (rows > 0) {
            SysMsgEntity msgEntity = new SysMsgEntity();
            String Id = moduleData.getId();
            msgEntity.setDataId(Id);
            msgEntity.setDataUrl("../module/moduledata.html?Id=" + Id);
            sysMsgService.addApproveItem(ProcessInfoEnum.MDE_MJZL, msgEntity, 0);
        }
        return rows;
    }

    @Override
    public int review(ModuleDataEntity moduleData) {
        moduleData.setApprovalUser(getUserId());
        moduleData.setApprovalTime(new Date());
        moduleData.setApprovalStatus(1);
        //添加审核 消息提醒
        sysProcessInfoService.saveProcessInfo(moduleData.getId(), ProcessInfoEnum.MDE_MJZL, moduleData.getApprovalStatus(), null);
        int rows = moduleDataDao.update(moduleData);
        if (rows > 0) {
            SysMsgEntity msgEntity = new SysMsgEntity();
            String Id = moduleData.getId();
            msgEntity.setDataId(Id);
            msgEntity.setIsRead(0);
            msgEntity.setDataUrl("../module/moduledata.html?Id=" + Id);
            sysMsgService.addApproveItem(ProcessInfoEnum.MDE_MJZL, msgEntity, 1);
        }
        return rows;
    }

    @Override
    public int recheck(String id, String knowledge, String reviewStatus) {
        ModuleDataEntity moduleData = moduleDataDao.queryObject(id);
        moduleData.setReviewUser(getUserId());
        moduleData.setReviewTime(new Date());
        moduleData.setReviewStatus(Integer.valueOf(reviewStatus));
        moduleData.setKnowledge(Integer.valueOf(knowledge));

        //添加知识库
        if (knowledge.equals("1")) {
            KnowBaseEntity knowBase = new KnowBaseEntity();
            knowBase.setDevCode(moduleData.getModuleCode());
            knowBase.setType("2");//模具
            knowBase.setTitle(moduleData.getFileName());
            knowBase.setIsDel(1);
            knowBase.setCreateUser(getUserId());
            knowBase.setCreateDate(new Date());
            knowBaseService.save(knowBase);

            //添加附件关联表
            Map<String, Object> map = new HashMap<>();
            map.put("moduleCode", moduleData.getModuleCode());
            List<ModuleDataFileEntity> moduleList = moduleDataFileService.queryModuleList(map);
            if (moduleList != null && moduleList.size() != 0) {
                for (ModuleDataFileEntity moduleDataFile : moduleList) {
                    KnowFileEntity knowFile = new KnowFileEntity();
                    knowFile.setFileId(moduleDataFile.getFileId());
                    knowFile.setKnowId(knowBase.getId());
                    knowFile.setType("2");//模具
                    knowFileService.save(knowFile);
                }
            }
        }
        int rows = moduleDataDao.update(moduleData);
        if (rows > 0) {
            SysMsgEntity sysMsgEntity = new SysMsgEntity();
            String Id = moduleData.getId();
            sysMsgEntity.setDataId(Id);
            sysMsgEntity.setDataType(ProcessInfoEnum.MDE_MJZL);
            sysMsgEntity.setDataFlag(SysMsgType.DATA_DEL_END);
            sysMsgService.allMsgData(sysMsgEntity);
        }
        return rows;
    }

    @Override
    public int delete(String id) {
        return moduleDataDao.delete(id);
    }

    @Override
    public int deleteBatch(String[] ids) {
        return moduleDataDao.deleteBatch(ids);
    }
}
